package com.ljxt.dt.service.storemanagementservice.impl;

import com.ljxt.dt.dao.storemanagementdao.RepertoryDao;
import com.ljxt.dt.dao.storemanagementdao.impl.RepertoryDaoImpl;
import com.ljxt.dt.domain.GoodsInfo;
import com.ljxt.dt.domain.Stock;
import com.ljxt.dt.domain.StockDetail;
import com.ljxt.dt.service.storemanagementservice.RepertoryService;
import com.ljxt.dt.service.storemanagementservice.StoreService;
import com.ljxt.dt.util.DaoUtil;
import com.ljxt.dt.util.ServiceUtil;
import com.ljxt.dt.util.ServletUtil;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.RepertorySelectVO;
import com.ljxt.dt.vo.storemanagement.RepertoryVO;
import com.ljxt.dt.vo.storemanagement.StockVO;
import com.ljxt.dt.web.servlet.storemanagementservlet.Repertory;


import java.util.ArrayList;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-20 9:47
 * @copyright 老九学堂
 */

public class RepertoryServiceImpl implements RepertoryService {
    private RepertoryDao admin = new RepertoryDaoImpl();
    @Override
    public PageVO<RepertoryVO> selectRepertory(RepertorySelectVO repertorySelectVO,Integer pageIndex,Integer pageSize) {

        //查询库存信息
        List<Stock> stocks = admin.selectStock(repertorySelectVO,pageIndex,pageSize);

        //创建一个RepertoryVO的集合
        List<RepertoryVO> repertoryVOS = new ArrayList<>();
        if(stocks != null){
            //循环添加
            for (Stock stock : stocks) {



                RepertoryVO repertoryVO = new RepertoryVO();

                //设置物品ID
                repertoryVO.setId(stock.getGoodsId());
                //设置物品名称
                repertoryVO.setGoodsName(stock.getGoodsName());
                //设置计量单位
                repertoryVO.setUnit(stock.getUnit());
                //设置最后更新时间
                repertoryVO.setLastDate(ServiceUtil.dateFormat(stock.getLastdate()));
                //设置所属仓库
                //查询到所属仓库
                List<String> names = DaoUtil.querySelectStringAll("select name FROM warehouse where id in((select warehouse_id from stock_detail where goods_id = ? GROUP BY warehouse_id))",stock.getGoodsId());

                repertoryVO.setWarehouse(names);
                //设置所属供应商

                String gys = DaoUtil.querySelectString("select supplier_name from supplier_info where id = (select supplier_id  from supplier_goods where goods_id = ?)",stock.getGoodsId());
                repertoryVO.setGys(gys);
                //设置可用库存
                Integer sum = Integer.parseInt(DaoUtil.querySelectString("select sum(total) from stock_detail where goods_id = ?",stock.getGoodsId()));
                repertoryVO.setAvailableStock(sum);
                //设置损坏数
                //设置图片
                String picture = DaoUtil.querySelectString("select picture from goods_info where id = ?",stock.getGoodsId());
                repertoryVO.setPicture(picture);
                Integer breakNum = Integer.parseInt(DaoUtil.querySelectString("select sum(broken_amount) from stock_detail where goods_id = ?",stock.getGoodsId()));
                repertoryVO.setBreakNum(breakNum);
                repertoryVOS.add(repertoryVO);

            }
            //查询总条数
            Integer totle = admin.totalCount(repertorySelectVO,pageIndex,pageSize);


            PageVO pageVO = new PageVO(repertoryVOS,pageIndex,pageSize,totle);

            return pageVO;
        }else {
            return new PageVO<>(null,pageIndex,pageSize,0);
        }



    }

    @Override
    public PageVO getStocks(Integer pageIndex, Integer pageSize, Integer id) {
       List<StockDetail>  list =  admin.getStocks(pageIndex,pageSize,id);
        //创建对应的StockVO
        List<StockVO> stockVOS = new ArrayList<>();

        if(list == null){
            list = new ArrayList<>();
    }
        for (StockDetail stockDetail : list) {
            StockVO stockVO = new StockVO();
            stockVO = (StockVO) ServiceUtil.setObj(stockDetail,stockVO);
            //获取仓库和货架信息
            String warehouse =  DaoUtil.querySelectString("select name from warehouse where id = ? ",stockDetail.getWarehouseId());
            String shelves = DaoUtil.querySelectString("  select name from shelves where id = ?",stockDetail.getShelvesId());
            //设置进去
            stockVO.setWarehouse(warehouse);
            stockVO.setShelves(shelves);
            //获取对应的颜色和尺寸
            String c = stockDetail.getAttrId();
            if("".equals(c) || c == null){

            }else {
                //判断是否有多个属性
                if(c.indexOf(",") != -1){
                    //按照,拆分
                    String[] attrs = c.split(",");
                    //循环获取值
                    for (String attr : attrs) {
                        //再根据-拆分
                        String[] at = attr.split("-");

                        //获取属性
                        String key = DaoUtil.querySelectString("select category_name from category_info where id = ? ",at[0]);
                        String value = DaoUtil.querySelectString("select attr_name from attr_info where id = ?",at[1]);
                        if("颜色".equals(key)){
                            stockVO.setColor(value);
                        }else if("尺码".equals(key)){
                            stockVO.setSize(value);
                        }
                    }
                }else{
                    //这则是只有一个属性
                    //直接按照-拆分
                    String[] at = c.split("-");
                    //获取属性
                    String key = DaoUtil.querySelectString("select name from category_info where id = ? ",at[0]);
                    String value = DaoUtil.querySelectString("select attr_name from attr_info where id = ?",at[1]);
                    if("颜色".equals(key)){
                        stockVO.setColor(value);
                    }else if("尺码".equals(key)){
                        stockVO.setSize(value);
                    }
                }
            }


            stockVOS.add(stockVO);

        }
        //获取总数
        Integer total = DaoUtil.querySelectCount("select count(*) from stock_detail where goods_id = ?",id);
        PageVO pageVO = new PageVO(stockVOS,pageIndex,pageSize,total);
        return pageVO;
    }


}
